package demo.liu.one.configs.rabbitmq;

import com.beust.jcommander.internal.Maps;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author by Liujt
 * @Classname MqConfig
 * @Description TODO
 * @Date 2021/6/21 17:19
 */
@Configuration
public class MqConfig {

    //------------------------------------------------direct--------------------------------------------------
    public static final String ITEM_DIRECT_QUEUE = "direct_queue";

    @Bean
    public Queue Queue() {
        /*Map<String, Object> argsMap = Maps.newHashMap();
        argsMap.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        argsMap.put("x-dead-letter-routing-key", DEAD_ROUTING);
        return new Queue(ITEM_DIRECT_QUEUE, true, false, false,argsMap);*/
        return new Queue(ITEM_DIRECT_QUEUE, true, false, false);
    }


    //------------------------------------------------topic--------------------------------------------------
    //交换机名称
    public static final String ITEM_TOPIC_EXCHANGE = "item_topic_exchange";
    //队列名称
    public static final String ITEM_QUEUE = "item_queue";

    //“*”操作符将“.”视为分隔符，匹配单个字符；“#”操作符没有分块的概念，它将任意“.”均视为关键字的匹配部分，能够匹配多个字符
    public static final String TOPIC_ROUTING_KEY = "item.#";

    //声明交换机
    @Bean("itemTopicExchange")
    public Exchange topicExchange() {
        return ExchangeBuilder.topicExchange(ITEM_TOPIC_EXCHANGE).durable(true).build();
    }

    //声明队列
    @Bean("itemQueue")
    public Queue itemQueue() {
        return QueueBuilder.durable(ITEM_QUEUE).build();
    }

    //绑定队列和交换机
    @Bean
    public Binding itemQueueExchange(@Qualifier("itemQueue") Queue queue,
                                     @Qualifier("itemTopicExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(TOPIC_ROUTING_KEY).noargs();
    }


    //------------------------------------------------fanout/广播模式--------------------------------------------------
    public static final String A_MESSAGE_QUEUE = "fanout.A";
    public static final String B_MESSAGE_QUEUE = "fanout.B";
    public static final String C_MESSAGE_QUEUE = "fanout.C";

    public static final String FANOUT_EXCHANGE = "fanoutExchange";

    @Bean(name = "amessage")
    public Queue AMessage() {
        return new Queue(A_MESSAGE_QUEUE);
    }

    @Bean(name = "bmessage")
    public Queue BMessage() {
        return new Queue(B_MESSAGE_QUEUE);
    }

    @Bean(name = "cmessage")
    public Queue CMessage() {
        return new Queue(C_MESSAGE_QUEUE);
    }

    //配置广播路由器
    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange(FANOUT_EXCHANGE);
    }

    @Bean
    Binding bindingExchangeA(@Qualifier("amessage") Queue AMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(AMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeB(@Qualifier("bmessage") Queue BMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(BMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeC(@Qualifier("cmessage") Queue CMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(CMessage).to(fanoutExchange);
    }




    /*------------------------死信队列DEMO------------------------*/
    /*定义两组队列信息*/
    /*1.消息队列*/
    /*2.死信队列*/
    /*3.消息队列绑定死信队列*/

    //正常队列配置
    public static final String COMMON_EXCHANGE = "mq.kill.item.success.kill.dead.prod.exchange";
    public static final String COMMON_QUEUE = "mq.kill.item.success.kill.dead.queue";
    public static final String COMMON_ROUTING = "mq.kill.item.success.kill.dead.prod.routing.key";

    //死信队列配置
    public static final String DEAD_EXCHANGE = "mq.kill.item.success.kill.dead.exchange";
    public static final String DEAD_QUEUE = "mq.kill.item.success.kill.dead.real.queue";
    public static final String DEAD_ROUTING = "mq.kill.item.success.kill.dead.routing.key";

    @Bean
    public Queue successKillDeadQueue() {
        Map<String, Object> argsMap = Maps.newHashMap();
        argsMap.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        argsMap.put("x-dead-letter-routing-key", DEAD_ROUTING);
        return new Queue(COMMON_QUEUE, true, false, false, argsMap);
    }

    //基本交换机
    @Bean
    public TopicExchange successKillDeadProdExchange() {
        return new TopicExchange(COMMON_EXCHANGE, true, false);
    }

    //创建基本交换机+基本路由 -> 死信队列 的绑定
    @Bean
    public Binding successKillDeadProdBinding() {
        return BindingBuilder.bind(successKillDeadQueue()).to(successKillDeadProdExchange())
                .with(COMMON_ROUTING);
    }

    //真正的队列
    @Bean
    public Queue successKillRealQueue() {
        return new Queue(DEAD_QUEUE, true);
    }

    //死信交换机
    @Bean
    public TopicExchange successKillDeadExchange() {
        return new TopicExchange(DEAD_EXCHANGE, true, false);
    }

    //死信交换机+死信路由->真正队列 的绑定
    @Bean
    public Binding successKillDeadBinding() {
        return BindingBuilder.bind(successKillRealQueue()).to(successKillDeadExchange()).with(DEAD_ROUTING);
    }

}
